import moment from "moment";
import typeJudgment from "@/utils/typeJudgment";
import {cEval} from "@/utils/utils";

function getHolidays(start:Date,end:Date){

  const Holidays = [
    '2021-11-06','2021-11-07',
    '2021-11-13','2021-11-14',
    '2021-11-20','2021-11-21',
    '2021-11-27','2021-11-28',
    '2021-12-04','2021-12-05'
  ]
  
  let hdays = 0;
  const oneDay = 86400000;
  const s = moment(start).valueOf();
  const e = moment(end).valueOf();
  Holidays.forEach(v =>{
    const h = moment(v).valueOf();
    if(s<=h && e >= h){
      // console.log(v);
      hdays ++;
    }
  })
  return hdays;
}


enum DateType {
  d="days",
  y='years',
  M='months',
  h='hours',
  m='minutes',
  s='seconds'
}

const methods = {
  'DATEDIF':function(start:any,end:any,type:string){

    // console.log({type:DateType[type]})
    start = parseFloat(start);
    end = parseFloat(end);
    const typeStr = DateType[type]

    if(!isNaN(start)&&!isNaN(end)) {
      let result =  moment(end).diff(moment(start),typeStr);
      if(type === 'd') result += 1;
      return  isNaN(result) ? '' : result;
    }
    return null
  },
  'SUM':function(){
    let num = 0;
    for(let i = 0;i<arguments.length;i++){
      const n = arguments[i]
      if(!isNaN(n)){
        num += parseFloat(n);
      }
    } 
    // console.log({num})
    return !isNaN(num) ? num : '';
  },
  'IF':function(check:any,trueVal:any = true,falseVal:any = false){
     
    try{
      return cEval(check) ? trueVal : falseVal
    }catch(err){
      return null;
    }
    
  },
  'DATE':function(val:any){
    console.log('DATE val',val)
    return moment(val).format();
  },
  'CONCATENATE':function(){
    let str = '';
    for(let i = 0;i<arguments.length;i++){
      str += arguments[i]
    } 
    return str
  },
  'ABS':function(val:any){
    // val = parseFloat(val);
    if(!isNaN(val)){
      return Math.abs(val);
    }
    return null;
  },
  'AVERAGE':function(){
    // val = parseFloat(val);
    let n = 0;
    for(let i = 0;i<arguments.length;i++){
      const item = parseFloat(arguments[i]);
      if(!isNaN(item)){
        n += item;
      }
    } 
    return n / arguments.length;
  },
  'COUNT':function(){
    return arguments.length;
  },
  "CEILING":function(t:any,e:any){
    t= parseFloat(t);
    e= parseFloat(e);
    if(!isNaN(t) && !isNaN(e)){
      return Math.ceil(t / e) * e; 
    }
    return null;
  },
  "COUNTIF":function(arr:any,check:any){
    return arr.filter((v:any)=> {
      if(!isNaN(v)){
        return cEval(v+check)
      }
    })
  },
  'FIXED':function(num:any,len:any){
    return !isNaN(num) ? parseFloat(num).toFixed(len) : null;
  },
  'FLOOR':function(n1:any,n2:any){
    n1= parseFloat(n2);
    n2= parseFloat(n2);
    if(!isNaN(n1) && !isNaN(n2)){
      return  Math.floor(n1 / n2) * n2; 
    }
    return null;
  },
  'MOD':function(n1:any,n2:any){
    n1= parseFloat(n2);
    n2= parseFloat(n2);
    if(!isNaN(n1) && !isNaN(n2)){
      return n1 % n2; 
    }
    return null;
  },
  'INT':function(num:any){
    num = parseInt(num);
    return !isNaN(num) ? num : null;
  },
  'LOG':function(t:any, e:any) {
    t = parseFloat(t);
    e = parseFloat(e);
    if(!isNaN(t) && !isNaN(e)){
      return typeJudgment.isNumber(e) ? Math.log(t) / Math.log(e) : 0;
    }
    return null
  },
  'MAX':function(){
    let nums = Array.from(arguments).filter(v=>{
      return !isNaN(v) && (typeJudgment.isNumber(v) || typeJudgment.isString(v))
    });
    return Math.max(...nums);
  },
  'MIN':function(){
    let nums = Array.from(arguments).filter(v=>{
      return !isNaN(v) && (typeJudgment.isNumber(v) || typeJudgment.isString(v))
    });
    return Math.min(...nums);
  },
  'POWER':function(t:any, e:any){
    e = Math.pow(t, e);
    return isNaN(e) ? 0 : e;
  },
  "PRODUCT":function(){
    let num = 1;
    for(let i = 0;i<arguments.length;i++){
      const item = parseFloat(arguments[i]);
      if(!isNaN(item)){
        num = num * item;
      }
    } 
    return num
  },
  "RAND":function(){
    return (Math.random() * 1).toFixed(6)
  },
  "ROUND":function(t:any, e:any) {
    t = parseFloat(t);
    e = parseFloat(e);
    if(!isNaN(t) && !isNaN(e)){
      let n:any = 1 .toFixed(e).replace('.','');
		 	var num = t * n;
			return Math.round(num) / n;
    }
    return null
  },
  "SQRT":function(n:any){
    return n < 0 ? 0 : Math.sqrt(n)
  },
  "SUMPRODUCT":function(t:any , e:any){
    var res:number[] = [];
    t.forEach((v:any,i:number)=>{
      res.push(v*e[i]);
    })
    var n = 0;
    res.forEach(v=>n+=v);
    return n ;
  },
  "CHAR":function(t:any){
    return String.fromCharCode(t);
  },
  "EXACT":function(s1:any,s2:any){
    return s1 === s2;
  },
  "ISEMPTY":function(t:any){
    return !t;
  },
  "LEFT":function(t:any,l:any){
    return t.substring(0,l);
  },
  "MID":function(t:any,s:any,l:any){
    return t.substring(s-1,s+l);	
  },
  "REPLACE":function(t:any,s:any,l:any,n:any){
    const str = t.substring(s-1,s+l);	
    return t.replace(str,n);
  },
  "LEN":function(t:any){
    return t.length;
  },
  "LOWER":function(t:any){
    return t.toLowerCase();
  },
  "UPPER":function(t:any){
    return t.toUpperCase();
  },
  "REPT":function(s:any,l:any){
    let str = s;
    for(let i=0;i<l-1;i++){
      str += s;
    }
    return str
  },
  "RIGHT":function(t:any,l:any){
    return t.substring(t.length - l);
  },
  "SEARCH":function(t:any, e:any, i:any){
    return e.toLowerCase().indexOf(t.toLowerCase(), i - 1) + 1;
  },
  "SPLIT":function(t:any, e:any){
    return typeJudgment.isString(t) ? t.split(e) : []
  },
  "TEXT":function(n:any){
    return n.toString()
  },
  "VALUE":function(n:any){
    const num = parseFloat(n);
    return !isNaN(num) ? num : null;
  },
  "TRIM":function(t:any){
    return typeJudgment.isString(t) ? t.replace(/ +/g, " ").trim() : ""
  },
  "UNION":function(){
    if(arguments[0].constructor === Array) {
      return Array.from(new Set(arguments[0]))
    }
    return Array.from(new Set(arguments))
  },
  "DAY":function(d:any){
    d = parseFloat(d);
    return !isNaN(d) ? moment(d).date() : null;
  },
  "DAYS":function(s:any,e:any){
    s=parseFloat(s);
    e=parseFloat(e);
    if(!isNaN(s)&&!isNaN(e)){
      return moment(e).diff(moment(s),'days');
    }
    return null
  },
  "DAYS360":function(s:any,e:any){
    s=parseFloat(s);
    e=parseFloat(e);
    if(!isNaN(s)&&!isNaN(e)){
      return moment(e).diff(moment(s),'days');
    }
    return null
  },
  "HOUR":function(d:any){
    d = parseFloat(d);
    return !isNaN(d) ? moment(d).hours() : null;
  },
  "ISOWEEKNUM":function(d:any){
    d = parseFloat(d);
    return !isNaN(d) ? moment(d).isoWeekday() : null;
  },
  "AND":function(){
    try{
      let res = [];
      for(let i = 0;i<arguments.length;i++){
        const item = arguments[i];
        res.push(cEval(item))
      } 
      // console.log({res})
      return res.every(v=>v);
    }catch(err){
      return null
    }
  },

  
  

}
export default methods;
